package com.n9.qqonline.api;

import com.n9.sdk.response.DefaultResponse;
import com.n9.sdk.response.PatentsResponse;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 软件用户跟服务端交互的API接口
 * @author ice
 * @since 2019/5/30 9:40
 */
public class UserApi {
    /**
     * 登陆
     * @param username
     * @param password
     * @return
     */
    public boolean login(String username, String password) {
        DefaultResponse login = NineIPFactory.create().login(username, password);
        return login.isOk();
    }

    /**
     * 获取所有可售专利
     */
    public List<PatentVO> getAllCanSell() {
        PatentsResponse resp = (PatentsResponse) NineIPFactory.create().getPatents();

        if (resp.isOk()) {
            return resp.getData().stream().map(patent -> {
                PatentVO pv = new PatentVO();
                pv.setNo(patent.getNumber());
                pv.setName(patent.getName());
                pv.setStatus(patent.getLegalStatus());
                pv.setSaleStatus(StockStatus.valueOfCode(patent.getStockStatus()).getDesc());
                pv.setPriority(0);
                String[] split = patent.getTags().split("[+=;；:：,，、\\s\t\n]");
                pv.setTagNames(split);
                pv.setTagCount(split.length);

                String expireDate = StringUtils.isEmpty(patent.getPaymentDeadline()) ? "无" : patent.getPaymentDeadline();
                pv.setExpireDate(expireDate);
                return pv;
            }).collect(Collectors.toList());
        }
        return new ArrayList<>(0);
    }
}
